const BasePuller = require('./BasePuller');
// 主目录数据解析
const ParserFactory = require('../Parsers/ParserFactory');
const fs = require('fs');

class CatalogPuller extends BasePuller {
    constructor() {
        super();
    }
    pull(url) {
        this.getPull(url)
            .then(res => {
                let catalogsParser = new ParserFactory('CatalogsParser');
                let data = catalogsParser.parser(res);

                // 暂时没有合理的组织方式
                // console.log(data);
                let that = this;
                (async function() {
                    function sleep(time) {
                        return new Promise((resolve, reject) => {
                            setTimeout(resolve, time);
                        })
                    }
                    let index = 0
                    for (let item of data) {
                        let subItems = item.subject_items;
                        for (const subItem of subItems) {
                            // console.log(subItem);
                            let questionsPuller = new ParserFactory('QuestionsPuller');
                            questionsPuller.pull(that.BASE_URL + subItem.qst_url)
                                .then(data => {
                                    // console.log(data);
                                    // fs.writeFile('./data/' + subItem.title + '.json', err => {
                                    //     if (err) {
                                    //         console.log(err);
                                    //     }
                                    // })
                                })
                            await sleep(2000)
                        }
                    }
                })(data)
            })
            .catch(err => {
                throw err;
            })
    }
}

module.exports = CataLogPuller;